package org.funtester.performance.books.chapter02.section3;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 阻塞锁示例
 */
public class BlockingLockDemo {

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();// 创建一个可重入锁
        Thread lockTestThread = new Thread(() -> {// 创建一个线程
            System.out.println(System.currentTimeMillis() + "  异步线程启动!  " + Thread.currentThread().getName());// 打印日志
            lock.lock();// 获取锁
            System.out.println(System.currentTimeMillis() + "  获取到锁了!  " + Thread.currentThread().getName());// 打印日志
            lock.unlock();// 释放锁
        });
        lock.lock();// 获取锁
        lockTestThread.start();// 启动异步线程
        Thread.sleep(100);// 睡眠100毫秒
        System.out.println(System.currentTimeMillis() + "  即将释放锁!  " + Thread.currentThread().getName());// 打印日志
        lock.unlock();// 释放锁
    }

}
